package com.lewei.print;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.PrinterName;

import com.lewei.utils.Constants;

/**
 * 
 * OL35数据打印模型
 * 
 * @author 22901
 * 
 */
public class OL35 implements Printable {
	/** 标签样式 */
	private String lenFlag = "G";
	/** 试验单位 */
	private String testingAgency = "ETC";
	/** 日期 */
	private String prodDatea = "2017-02-09";

	/** 编号 */
	// private String typeNo="900540G23R6SA15N";
	private String typeNo = "900540G23R6SA15N";

	private String typeNo1 = "900540G23R6SA15N";
	/** 出厂编号 */
	private String serialNo = "200052-951";

	/** 限速器名称 */
	private String elevatorNo = "OL35";

	/** Vns */
	private String vns = "";

	/** Vcs */
	private String vcs = "";

	/** Vts */
	private String vts = "";

	/** 钢丝绳粗细 */
	private String lineThickness = "6.0";

	/** FbrakeMin提拉力 */
	private String fbrakeMin = "500";
	
	/** tensionForce张紧力 */
	private String tensionForce = "125";

	/** 标签样式 */
	private String labelType = "EN81";

	/** 型号 */
	// private String modelNo="900540";
	private String modelNo = "80420";

	/** 证书编号 */
	// private String certificateNo="TUV-A-AT-1-99-002-CEGB-6";
	private String certificateNo = "TUV-A-AT-1-98-001-CEGB-6";

	private String technicalParameters = "CAR";

	/** 左边距 */
	private double paddingLeft = 10;

	/** 上边距 */
	private double paddingTop = 20;

	/** 打印机名称 */
	private String printerName = "Microsoft Print to PDF";

	public String getTechnicalParameters() {
		return technicalParameters;
	}

	public void setTechnicalParameters(String technicalParameters) {
		this.technicalParameters = technicalParameters;
	}

	public String getTestingAgency() {
		return testingAgency;
	}

	public void setTestingAgency(String testingAgency) {
		this.testingAgency = testingAgency;
	}

	public String getLenFlag() {
		return lenFlag;
	}

	public void setLenFlag(String lenFlag) {
		this.lenFlag = lenFlag;
	}

	public String getProdDatea() {
		return prodDatea;
	}

	public void setProdDatea(String prodDatea) {
		this.prodDatea = prodDatea;
	}

	public String getTypeNo() {
		return typeNo;
	}

	public void setTypeNo(String typeNo) {
		this.typeNo = typeNo;
	}

	public String getTypeNo1() {
		return typeNo1;
	}

	public void setTypeNo1(String typeNo1) {
		this.typeNo1 = typeNo1;
	}

	public String getSerialNo() {
		return serialNo;
	}

	public void setSerialNo(String serialNo) {
		this.serialNo = serialNo;
	}

	public String getElevatorNo() {
		return elevatorNo;
	}

	public void setElevatorNo(String elevatorNo) {
		this.elevatorNo = elevatorNo;
	}

	public String getVns() {
		return vns;
	}

	public void setVns(String vns) {
		this.vns = vns;
	}

	public String getVcs() {
		return vcs;
	}

	public void setVcs(String vcs) {
		this.vcs = vcs;
	}

	public String getVts() {
		return vts;
	}

	public void setVts(String vts) {
		this.vts = vts;
	}

	public String getLineThickness() {
		return lineThickness;
	}

	public void setLineThickness(String lineThickness) {
		this.lineThickness = lineThickness;
	}

	public String getFbrakeMin() {
		return fbrakeMin;
	}

	public void setFbrakeMin(String fbrakeMin) {
		this.fbrakeMin = fbrakeMin;
	}

	public String getLabelType() {
		return labelType;
	}

	public void setLabelType(String labelType) {
		this.labelType = labelType;
	}

	public String getModelNo() {
		return modelNo;
	}

	public void setModelNo(String modelNo) {
		this.modelNo = modelNo;
	}

	public String getCertificateNo() {
		return certificateNo;
	}

	public void setCertificateNo(String certificateNo) {
		this.certificateNo = certificateNo;
	}

	public double getPaddingLeft() {
		return paddingLeft;
	}

	public void setPaddingLeft(double paddingLeft) {
		this.paddingLeft = paddingLeft;
	}

	public double getPaddingTop() {
		return paddingTop;
	}

	public void setPaddingTop(double paddingTop) {
		this.paddingTop = paddingTop;
	}

	public String getPrinterName() {
		return printerName;
	}

	public void setPrinterName(String printerName) {
		this.printerName = printerName;
	}

	/**
	 * 设置边距。
	 * 
	 * @param paddingLeft
	 * @param paddingTop
	 */
	public void setPadding(double paddingLeft, double paddingTop) {
		this.paddingLeft = paddingLeft;
		this.paddingTop = paddingTop;
	}

	public OL35(String printerName) {
		super();
		this.printerName = printerName;
	}

	public OL35() {
		super();
	}

	/**
	 * @param Graphic指明打印的图形环境
	 * @param PageFormat指明打印页格式
	 *            （页面大小以点为计量单位，1点为1英才的1/72，1英寸为25.4毫米。A4纸(210mm×297mm)大致为595×
	 *            842点）比例约等于2.83
	 * @param pageIndex指明页号
	 **/

	@Override
	public int print(Graphics gra, PageFormat pf, int pageIndex)
			throws PrinterException {
		// TODO Auto-generated method stub
		Component c = null;
		// 转换成Graphics2D
		Graphics2D g2 = (Graphics2D) gra;
		// 设置打印颜色为黑色
		g2.setColor(Color.black);

		// 打印起点坐标
		double x = pf.getImageableX() + 60;
		double y = pf.getImageableY();

		switch (pageIndex) {
		case 0:
			// 英文
			if (lenFlag.equals("E") || lenFlag.equals("U")) {
				Font font = new Font("Calibri", Font.BOLD, 9);
				g2.setFont(font);
				g2.drawString(typeNo, (float) (x + 20), (float) (y + 25));
				String filename = "C:\\toolsZ\\labelPrint\\codeZ\\typeNo.jpg";
				Image Img = null;
				try {
					Img = ImageIO.read(new File(filename));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				g2.drawImage(Img, (int) x + 130, (int) y + 15, 55, 55, c);
			    font = new Font("Calibri", Font.BOLD, 8);
				g2.setFont(font);
				g2.drawString(serialNo, (float) (x + 50), (float) (y + 60));// 中间的出厂编号200066_XXX
				filename = "C:\\toolsZ\\labelPrint\\codeZ\\serialNo.jpg";
				try {
					Img = ImageIO.read(new File(filename));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				g2.drawImage(Img, (int) x - 5, (int) y + 40, 55, 55, c);
				 font = new Font("Calibri", Font.BOLD, 9);
				g2.setFont(font);
				g2.drawString(elevatorNo, (float) (x + 90), (float) (y + 90));// 中间的OL35

				font = new Font("Calibri", Font.BOLD, 9);
				g2.setFont(font);
				if (lenFlag.equals("E")) {
					labelType = "EN81";
					g2.drawString(labelType, (float) (x - 33), (float) (y + 110));// 标签样式左侧左下角
				} else if (lenFlag.equals("U")) {
					font = new Font("Calibri", Font.BOLD, 6);
					g2.setFont(font);
					labelType = "EN81-20";
					g2.drawString(labelType, (float) (x - 35), (float) (y + 109));// 标签样式左侧左下角
				}
				font = new Font("Calibri", Font.BOLD, 8);
				g2.setFont(font);
				g2.drawString(vns, (float) (x - 4), (float) (y + 115));
				g2.drawString(vcs, (float) (x + 38), (float) (y + 115));
				g2.drawString(vts, (float) (x + 80), (float) (y + 115));

				font = new Font("Calibri", Font.BOLD,9);
				g2.setFont(font);
				g2.drawString(lineThickness, (float) (x + 148),
						(float) (y + 110));// 左侧右下角粗细6.0mm
				font = new Font("Calibri", Font.BOLD, 9);
				g2.setFont(font);
				g2.drawString(fbrakeMin, (float) (x - 30), (float) (y + 95));// 最左侧500N
				g2.drawString("80420", (float) (x - 30), (float) (y + 70));// 左侧图标下的80420
				font = new Font("Calibri", Font.BOLD, 5);
				g2.setFont(font);
				g2.drawString(prodDatea, (float) (x - 30), (float) (y + 25));// 左侧左上角时间
				font = new Font("Calibri", Font.BOLD, 7);
				g2.setFont(font);
				g2.drawString("80420", (float) (x + 240), (float) (y + 43));// 右侧的80420
				g2.drawString(certificateNo, (float) (x + 210),
						(float) (y + 52));// 右侧的证书
				g2.drawString(prodDatea, (float) (x + 230), (float) (y + 68));// 右侧的时间
				// 中文标签
			} else if (lenFlag.equals("G")) {
				
				Font font = new Font("微软雅黑", Font.BOLD, 9);
				g2.setFont(font);
				g2.drawString(typeNo, (float) (x + 20), (float) (y + 25));// 主编码
				String filename = "C:\\toolsZ\\labelPrint\\codeZ\\typeNo.jpg";
				Image Img = null;
				try {
					Img = ImageIO.read(new File(filename));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				g2.drawImage(Img, (int) x + 130, (int) y + 2, 50, 50, c);// ==========主编码二维码================
				
				font = new Font("微软雅黑", Font.BOLD, 7);
				g2.setFont(font);
				
				g2.drawString(serialNo, (float) (x + 46), (float) (y + 42));// 中间的出厂编号200066_XXX
				font = new Font("Calibri", Font.PLAIN, 9);
				g2.setFont(font);
				g2.drawString(prodDatea, (float) (x + 55), (float) (y + 62));//打印时间
				filename = "C:\\toolsZ\\labelPrint\\codeZ\\serialNo.jpg";
				try {
					Img = ImageIO.read(new File(filename));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				g2.drawImage(Img, (int) x - 42, (int) y + 23, 55, 55, c);// =========生产订单号二维码===================

				font = new Font("Calibri", Font.BOLD, 9);
				g2.setFont(font);
				g2.drawString(vns, (float) (x - 24), (float) (y + 116));
				g2.drawString(vcs, (float) (x + 27), (float) (y + 116));
				g2.drawString(vts, (float) (x + 78), (float) (y + 116));

				g2.drawString(lineThickness, (float) (x + 150),(float) (y + 116));//6.0mm
				font = new Font("Calibri", Font.BOLD, 9);
				g2.setFont(font);
				g2.drawString(tensionForce, (float) (x + 138), (float) (y + 70));//125N
				g2.drawString(fbrakeMin, (float) (x + 138), (float) (y + 97));//500N
				g2.drawString(elevatorNo, (float) (x - 20), (float) (y + 78));// OL35
				

				g2.drawString(elevatorNo, (float) (x + 228), (float) (y + 37));// 右侧的//OL35
				g2.drawString("TSX", (float) (x + 230), (float) (y + 53));// 右侧的TSX
				g2.drawString("ETC", (float) (x + 230), (float) (y + 68));// 右侧的ETC

				// //设置字体方向
				// g2.rotate(Math.toRadians(270));
				//
				// font = new Font("Calibri", Font.BOLD, 7);
				// g2.setFont(font);
				// g2.drawString(elevatorNo,(float) (-(y+78)),
				// (float) (x+217));
				// g2.drawString(certificateNo,(float) (-(y+110)),
				// (float) (x+228));
			}

			return PAGE_EXISTS;
		default:
			return NO_SUCH_PAGE;
		}
	}

	public static void main(String[] args) {

		// 通俗理解就是书、文档
		Book book = new Book();
		// 设置成竖打
		PageFormat pf = new PageFormat();
		pf.setOrientation(PageFormat.PORTRAIT);
		// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
		Paper p = new Paper();
		p.setSize(350, 210);// 纸张大小
		// A4(595 X 842)设置打印区域，其实0，0应该是72，72，因为A4纸的默认X,Y边距是72
		p.setImageableArea(0, 0, 350, 210);
		pf.setPaper(p);
		// 把 PageFormat 和 Printable 添加到书中，组成一个页面
		book.append(new OL35(), pf);

		// 获取打印服务对象
		PrinterJob job = PrinterJob.getPrinterJob();

		HashAttributeSet hs = new HashAttributeSet();

		String printerName = "Microsoft Print to PDF";
		// String printerName = "Send To OneNote 2016";
		hs.add(new PrinterName(printerName, null));

		PrintService[] pss = PrintServiceLookup.lookupPrintServices(null, hs);

		try {
			job.setPrintService(pss[0]);
		} catch (PrinterException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		// 设置打印类
		job.setPageable(book);

		try {
			// 可以用printDialog显示打印对话框，在用户确认后打印；也可以直接打印
			// boolean a=job.printDialog();
			// if(a)
			// {
			job.print();
			// }
		} catch (PrinterException e) {
			e.printStackTrace();
		}
	}

	public void printcode() {

		// 通俗理解就是书、文档
		Book book = new Book();

		PageFormat pageFormat = new PageFormat();
		// 打印方向
		pageFormat.setOrientation(PageFormat.PORTRAIT);
		// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
		Paper paper = new Paper();
		paper.setSize(400, 300);// 纸张大小
		// if(lenFlag.equals("E")){
		// // A4(595 X 842)设置打印区域，其实0，0应该是72，72，因为A4纸的默认X,Y边距是72
		// paper.setImageableArea(Constants.LABELLEFT900540E,
		// Constants.LABELTOP900540E, 400, 300);
		// }else if(lenFlag.equals("G")&&modelNo.equals("900540")){
		// paper.setImageableArea(Constants.LABELLEFT900540-3,
		// Constants.LABELTOP900540-5, 400, 300);
		// }else{
		paper.setImageableArea(Constants.LABELLEFT80420,
				Constants.LABELTOP80420, 400, 300);
		// }

		pageFormat.setPaper(paper);
		// 把 PageFormat 和 Printable 添加到书中，组成一个页面
		book.append(this, pageFormat);

		// 获取打印服务对象
		PrinterJob printJob = PrinterJob.getPrinterJob();

		HashAttributeSet hashAttributeSet = new HashAttributeSet();

		hashAttributeSet.add(new PrinterName(printerName, null));

		PrintService[] printServices = PrintServiceLookup.lookupPrintServices(
				null, hashAttributeSet);

		try {
			printJob.setPrintService(printServices[0]);
		} catch (PrinterException e1) {
			e1.printStackTrace();
			throw new RuntimeException(e1.getMessage());
		}

		// 设置打印类
		printJob.setPageable(book);
		try {
			// 可以用printDialog显示打印对话框，在用户确认后打印；也可以直接打印
			// boolean a=job.printDialog();
			// if(a)
			// {
			printJob.print();
			// }
		} catch (PrinterException e) {
			e.printStackTrace();
		}

	}

}
